package cn.tedu.micro.demo.rabbitmq.controller;

import cn.tedu.micro.demo.rabbitmq.amqp.ProducerClient;
import cn.tedu.micro.demo.rabbitmq.pojo.MyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SendController {
    /*@Autowired
    private RabbitTemplate rabbitTemplate;*/
    @Autowired
    private ProducerClient<MyMessage> producerClient;
    /**
     * GET
     * /SEND
     * String msg 消息体数据
     * 返回 "success"
     */
    @GetMapping("/send")
    public String send(String msg){
        log.info("收到参数消息:{}",msg);
        //channel.basicPublish("demo_ex","demo_rk",null,msg.getByte());
        /*//1.考虑消息的封装 消息包含消息体 msg 包含消息头(header交换机) 包含消息属性等
        //消息可以携带的固定属性 appId userId priority
        MessageProperties messageProperties = new MessageProperties();
        //存放固定属性
        messageProperties.setPriority(100);
        //存放消息头
        messageProperties.setHeader("name","wch");
        Message message = MessageBuilder
                .withBody(msg.getBytes(StandardCharsets.UTF_8))
                .andProperties(messageProperties).build();
        //2.考虑消息发送 发送目标 携带路由key convertAndSend
        rabbitTemplate.convertAndSend("demo_ex","demo_rk",message);
        */
        //直接发送封装的业务数据 不需要关心Message类型如何创建 如何封装
        /*MyMessage myMessage=new MyMessage();
        myMessage.setMsg(msg);
        rabbitTemplate.convertAndSend("demo_ex","demo_rk",myMessage);*/
        MyMessage myMessage=new MyMessage();
        myMessage.setMsg(msg);
        producerClient.send("demo_ex","demo_rk",myMessage);
        return "success";
    }
}
